﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Century.EnterpriseLibrary.Caching
{
    public class Encrypt
    {
        /// <summary>
        /// method to generate standard dh5 hash string.
        /// </summary>
        /// <param name="dataString"></param>
        /// <returns></returns>
        public static string ToMD5(string dataString)
        {
            if (string.IsNullOrEmpty(dataString))
            {
                return string.Empty;
            }
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.GetEncoding("ISO-8859-9").GetBytes(dataString);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }
    }
}
